/**
 * ReportGear(2011)
 */
package com.reportgear.designer.action;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;
import java.util.Map;

import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

import com.reportgear.designer.action.core.ActionUtils;
import com.reportgear.designer.gui.config.ShortCut;
import com.reportgear.designer.util.GUIConstants;

/**
 * 更新操作
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public abstract class UpdateAction extends ShortCut implements Action, Cloneable {
	private boolean enabled = true;
	private boolean isToggleButton = false;
	private transient Map<String, Object> componentMap;
	private PropertyChangeSupport changeSupport;

	public UpdateAction() {
		putValue("SmallIcon", GUIConstants.getBlankIcon());
	}

	public boolean isEnabled() {
		return this.enabled;
	}

	public void setEnabled(boolean enabled) {
		boolean bool = this.enabled;
		if (bool == enabled) {
			return;
		}

		this.enabled = enabled;
		for (Object c : this.componentMap.values()) {
			if (c instanceof JComponent)
				((JComponent) c).setEnabled(this.enabled);
		}
	}

	public boolean isToggleButton() {
		return this.isToggleButton;
	}

	public void setToggleButton(boolean isToggleButton) {
		this.isToggleButton = isToggleButton;
	}

	public String getName() {
		return ((String) getValue("Name"));
	}

	public void setName(String paramString) {
		putValue("Name", paramString);
	}

	public Icon getSmallIcon() {
		return ((Icon) getValue("SmallIcon"));
	}

	public void setSmallIcon(Icon paramIcon) {
		putValue("SmallIcon", paramIcon);
	}

	public char getMnemonic() {
		Integer localInteger = (Integer) getValue("MnemonicKey");
		return ((localInteger == null) ? '\0' : (char) localInteger.intValue());
	}

	public void setMnemonic(char paramChar) {
		putValue("MnemonicKey", new Integer(paramChar));
	}

	public KeyStroke getAccelerator() {
		return ((KeyStroke) getValue("AcceleratorKey"));
	}

	public void setAccelerator(KeyStroke keyStroke) {
		putValue("AcceleratorKey", keyStroke);
	}

	public void update() {
	}

	public Object getValue(String key) {
		if (this.componentMap == null)
			return null;
		return this.componentMap.get(key);
	}

	public void putValue(String key, Object value) {
		if (this.componentMap == null)
			this.componentMap = new HashMap<String, Object>();
		Object object = this.componentMap.get(key);
		if (value == null)
			this.componentMap.remove(key);
		else
			this.componentMap.put(key, value);

		firePropertyChange(key, object, value);
	}

	protected void firePropertyChange(String propertyName, Object oldName, Object newName) {
		if ((this.changeSupport == null) || ((oldName != null) && (newName != null) && (oldName.equals(newName))))
			return;

		this.changeSupport.firePropertyChange(propertyName, oldName, newName);
	}

	public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
		if (this.changeSupport == null)
			this.changeSupport = new PropertyChangeSupport(this);
		this.changeSupport.addPropertyChangeListener(listener);
	}

	public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
		if (this.changeSupport == null)
			return;
		this.changeSupport.removePropertyChangeListener(listener);
	}

	public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
		if (this.changeSupport == null)
			return new PropertyChangeListener[0];

		return this.changeSupport.getPropertyChangeListeners();
	}

	public JMenuItem createMenuItem() {
		Object obj = getValue(JMenuItem.class.getName());

		if ((obj == null) && (!(obj instanceof JMenuItem))) {
			obj = new JMenuItem(this);
			putValue(JMenuItem.class.getName(), obj);
		}

		return (JMenuItem) obj;
	}

	public JComponent createToolBarComponent() {
		Object obj = getValue(JButton.class.getName());
		if ((obj == null) || (!(obj instanceof AbstractButton))) {
			AbstractButton btn = null;
			if (isToggleButton())
				btn = new JToggleButton();
			else
				btn = new JButton();

			Integer mnemonicKey = (Integer) getValue("MnemonicKey");

			if (mnemonicKey != null)
				btn.setMnemonic((char) mnemonicKey.intValue());

			btn.setIcon((Icon) getValue("SmallIcon"));
			btn.addActionListener(this);

			putValue(JButton.class.getName(), btn);

			btn.setText("");
			btn.setEnabled(isEnabled());
			btn.setToolTipText(ActionUtils.createButtonToolTipText(this));
			obj = btn;
		}

		return (JComponent) obj;
	}

	public boolean intercept2JPopupMenu() {
		update();

		return !isEnabled();
	}

	public void intoJPopupMenu(JPopupMenu popupMenu) {
		if (!(isEnabled()))
			return;

		popupMenu.add(createMenuItem());
	}

	public boolean intercept2JToolBar() {
		update();

		return false;
	}

	public void intoJToolBar(JToolBar toolbar, Map<String, ButtonGroup> btnGroup) {
		JComponent c = createToolBarComponent();

		// TODO 处理单选
		// if ((this instanceof RadioAction) && (c instanceof AbstractButton)) {
		// RadioAction localRadioAction = (RadioAction) this;
		// String str = localRadioAction.getGroupName();
		// ButtonGroup group = (ButtonGroup) btnGroup.get(str);
		// if (group == null) {
		// group = new ButtonGroup();
		// btnGroup.put(str, group);
		// }
		// group.add((AbstractButton) c);
		// }

		toolbar.add(c);
	}

	// public UseMenuItem createUseMenuItem() {
	// Object obj = getValue(UseMenuItem.class.getName());
	// if ((obj == null) && (!(obj instanceof UseMenuItem))) {
	// obj = new UseMenuItem(this);
	//
	// putValue(UseMenuItem.class.getName(), obj);
	// }
	// return (UseMenuItem) obj;
	// }
	//
	// public static class UseMenuItem extends JMenuItem {
	// private NameStyle nameStyle;
	//
	// public UseMenuItem(Action action) {
	// super(action);
	//
	// setPreferredSize(new Dimension(80, 20));
	// }
	//
	// public UseMenuItem(String text, Icon action) {
	// super(text, action);
	// }
	//
	// public void paintComponent(Graphics g) {
	// super.paintComponent(g);
	// Graphics2D g2d = (Graphics2D) g;
	//
	// if (this.nameStyle == null) {
	// return;
	// }
	//
	// PaintUtils.paintBackground(g2d, this.nameStyle, getWidth() - 1,
	// getHeight() - 1);
	// PaintUtils.paintContent(g2d, this.nameStyle.getName(), this.nameStyle,
	// getWidth() - 1, getHeight() - 1);
	// PaintUtils.paintBorder(g2d, this.nameStyle, getWidth() - 1, getHeight() -
	// 1);
	// }
	//
	// public NameStyle getNameStyle() {
	// return this.nameStyle;
	// }
	//
	// public void setNameStyle(NameStyle paramNameStyle) {
	// this.nameStyle = paramNameStyle;
	// }
	//
	// public Dimension getMinimumSize() {
	// return getPreferredSize();
	// }
	// }
}